﻿using System;
using System.Collections.Generic;

namespace Genesis.Domain
{

    using Genesis.Infrastructure;
    using Genesis.Infrastructure.Domain;

    public class User : Entity
    {

        private IList<Favorite> favorites = new List<Favorite>();

        private IList<Subscription> subscriptions = new List<Subscription>();

        [Signature]
        public virtual string Username { get; protected set; }

        [Signature]
        public virtual string Password { get; protected set; }

        [Signature]
        public virtual string Email { get; protected set; }
        
        public virtual string DisplayName { get; set; }
        
        public virtual bool ShowEmail { get; set; }
        
        public virtual bool ShowDisplayName { get; set; }

        public virtual string HomePage { get; set; }

        public virtual string AimAddress { get; set; }
        
        public virtual string MsnAddress { get; set; }
        
        public virtual string YimAddress { get; set; }
        
        public virtual string AvatarUrl { get; set; }
        
        public virtual string Signature { get; set; }
        
        public virtual DateTime JoinDate { get; set; }

        public virtual string JoinAddress { get; set; }
        
        public virtual Guid ActivateCode { get; set; }
        
        public virtual DateTime ActivateDate { get; set; }

        public virtual string ActivateAddress { get; set; }
        
        public virtual DateTime LastVisitDate { get; set; }

        public virtual string LastVisitAddress { get; set; }

        public virtual DateTime LastSearchDate { get; set; }

        public virtual string LastSearchAddress { get; set; }

        public virtual int LoginAttempts { get; set; }

        public virtual bool IsLockedOut { get; set; }
        
        public virtual DateTime UnlockDate { get; set; }

        public virtual bool IsApproved { get; set; }

        public virtual IEnumerable<Favorite> Favorites { get { return favorites; } }

        public virtual IEnumerable<Subscription> Subscriptions { get { return subscriptions; } }

        public virtual void AddFavorite(Favorite favorite)
        {
            
            Logic.Check(favorite != null, "Favorite can not be null!");
            
            if (!favorites.Contains(favorite))
                favorites.Add(favorite);
        
        }

        public virtual void RemoveFavorite(Favorite favorite)
        {
        
            Logic.Check(favorite != null, "Favorite can not be null!");
            
            if (favorites.Contains(favorite))
                favorites.Remove(favorite);
        
        }

        public virtual void AddSubscription(Subscription subscription)
        {
        
            Logic.Check(subscription != null, "Subscription can not be null!");
            
            if (!subscriptions.Contains(subscription))
                subscriptions.Add(subscription);
        
        }

        public virtual void RemoveSubscription(Subscription subscription)
        {
        
            Logic.Check(subscription != null, "Subscription can not be null!");
            
            if (subscriptions.Contains(subscription))
                subscriptions.Remove(subscription);
        
        }

        public virtual void SetUsername(string username)
        {
        
            Logic.Check(!string.IsNullOrEmpty(username) && !string.IsNullOrWhiteSpace(username), "Username can not be null!");
            
            Username = username.Trim();
        
        }
        
        public virtual void SetPassword(string password)
        {
        
            Logic.Check(!string.IsNullOrEmpty(password) && !string.IsNullOrWhiteSpace(password), "Password can not be null!");
            
            Password = password.Trim();
        
        }
        
        public virtual void SetEmail(string email)
        {
        
            Logic.Check(!string.IsNullOrEmpty(email) && !string.IsNullOrWhiteSpace(email), "Email can not be null!");
            
            Email = email.Trim();
        
        }
        
        public User(string username, string password, string email)
        {
        
            SetUsername(username);
            SetPassword(password);
            SetEmail(email);
        
        }
        
        protected User() { }
    
    }

}